package com.springcloud.controller;

import com.springcloud.entities.CommonResult;
import com.springcloud.entities.LoginUser;
import com.springcloud.entities.User;
import com.springcloud.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

@RestController
@Slf4j
public class ProviderUserController {


	public static final String PAYMENT_URL = "http://CLOUD-STREAM-PROVIDER"; //加入Eureka后通过注册中心里的服务名来查找

	@Resource
	private RestTemplate restTemplate;
	@Resource
	private UserService userService;



	@PostMapping(value = "/user/create")
	public CommonResult createUser(@RequestBody User user) {
		int result = userService.createUser(user);
		log.info("插入结果："+result);

		if(result>0) {
			restTemplate.getForObject(PAYMENT_URL+"/sendMessage/"+user.getLoginName(),CommonResult.class);
			return new CommonResult(200,"插入数据成功！",result);
		}else {
			return new CommonResult(444,"插入数据失败！",null);
		}
	}

	@PostMapping(value = "/user/login")
	public CommonResult login(@RequestBody LoginUser user) {
		 User result = userService.login(user);

		if(result == null) {
			return new CommonResult(444,"帐号或者密码错误！",null);
		}else {
			return new CommonResult(200,"登录成功！",result);
		}
	}
	@PostMapping(value = "/user/getLoginPWD")
	public CommonResult getLoginPWD(@RequestBody User user) {
		User result = userService.getLoginPWD(user);

		if(result == null) {
			return new CommonResult(444,"帐号或者密码错误！",null);
		}else {
			return new CommonResult(200,"登录成功！",result);
		}
	}
}
